package com.erp.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.erp.dto.Department;
import com.erp.dto.Page;
import com.erp.util.DepartmentUtils;

@SuppressWarnings("serial")
@Controller(value = "departmentAction")
@Scope(value = "prototype")
public class DepartmentAction extends ModelDrivenBaseAction<Department> {

	/**
	 * 添加页面
	 */
	public String addPage() throws Exception {
		request.put("departmentList",
				DepartmentUtils.getDepartmentTree(departmentService.listAll()));
		return "addPage";
	}

	/**
	 * 添加操作
	 */
	public void add() throws Exception {
		departmentService.add(model);
		writeJson("msg", "true");
	}

	/**
	 * 删除操作
	 */
	public void delete() throws Exception {
		departmentService.delete(model);
		writeJson("msg", "true");
	}

	/**
	 * 编辑页面
	 */
	public String editPage() throws Exception {
		request.put("departmentList",
				DepartmentUtils.getDepartmentTree(departmentService.listAll()));
		model = departmentService.getById(model.getId());
		pushIntoValueStack(model);
		return "editPage";
	}

	/**
	 * 获取部门的描述
	 * 
	 * @throws Exception
	 */
	public void getDescription() throws Exception {
		model = departmentService.getById(model.getId());
		String description = model.getDescription();
		if (description == null || "".equals(description.trim())) {
			writeJson("description", "暂无描述");
		} else {
			writeJson("description", description);
		}
	}

	/**
	 * 编辑操作
	 */
	public void edit() throws Exception {
		departmentService.update(model);
		writeJson("msg", "true");
	}

	/**
	 * 部门列表
	 */
	public String list() throws Exception {
		if (page == null) {
			page = new Page();
		}
		return pagedList();
	}

	/**
	 * 所有部门列表
	 */
	public String listAll() throws Exception {
		request.put("userList", departmentService.listAll());
		return "list";
	}

	/**
	 * 分页后的部门列表
	 */
	public String pagedList() throws Exception {
		request.put("departmentList", departmentService.getPagedList(page));
		pushIntoValueStack(page);
		return "list";
	}
	
}
